using UnityEngine;
namespace DefaultNamespace
{
    public class SectorDetection : MonoBehaviour
    {
        [Header("扇形设置")] public float radius = 5f; // 扇形半径
        public float angle = 90f; // 扇形角度
        public LayerMask targetLayer; // 检测的目标层级

        void Update()
        {
            DetectTargetsInSector();
        }

        void DetectTargetsInSector()
        {
            // 获取扇形内的所有碰撞体
            Collider[] colliders = Physics.OverlapSphere(transform.position, radius, targetLayer);

            foreach (var collider in colliders) {
                // 计算目标与玩家之间的方向向量
                Vector3 directionToTarget = (collider.transform.position - transform.position).normalized;

                // 判断是否在扇形角度范围内 Vector3.Angle计算两个向量之间的夹角
                float angleToTarget = Vector3.Angle(transform.forward, directionToTarget);
                if (angleToTarget <= angle / 2) {
                    // 如果目标在范围内
                    Debug.Log($"目标 {collider.name} 在扇形范围内");
                    // 可在此处添加命中处理逻辑
                }
            }
        }

        void OnDrawGizmos()
        {
            // 绘制扇形区域
            Gizmos.color = new Color(1, 0, 0, 0.3f);
            Vector3 forward = transform.forward * radius;
            Quaternion leftRotation = Quaternion.Euler(0, -angle / 2, 0);
            Quaternion rightRotation = Quaternion.Euler(0, angle / 2, 0);
            Vector3 leftDirection = leftRotation * forward;
            Vector3 rightDirection = rightRotation * forward;

            // 绘制扇形范围
            Gizmos.DrawLine(transform.position, transform.position + leftDirection);
            Gizmos.DrawLine(transform.position, transform.position + rightDirection);
            Gizmos.DrawWireSphere(transform.position, radius);
        }
    }
}